package org.zzz.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.zzz.entity.Student;
import org.zzz.entity.StudentVo;
import org.zzz.entity.vo.StudentShowVo;
import org.zzz.mapper.StudentMapper;
import org.zzz.service.StudentCourseService;
import org.zzz.service.StudentService;
import org.zzz.utils.MybatisUtil;

import java.util.List;

/**
 * @Author ZZF
 * @Time 2021/04/20 22:47
 */
public class StudentServiceImpl implements StudentService {

    private StudentMapper studentMapper;
    private StudentCourseService studentCourseService = new StudentCourseServiceImpl();

    {
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
        studentMapper = MybatisUtil.getMapper(StudentMapper.class);
    }

    @Override
    public boolean insertStudent(Student student) {
        return studentMapper.insertStudent(student) > 0;
    }

    @Override
    public List<Student> listAllStudent() {
        return studentMapper.listAllStudent();
    }

    @Override
    public List<StudentShowVo> listAllStudentShow() {

        List<StudentShowVo> studentShowVos = studentMapper.listAllStudentShow();
        for (StudentShowVo studentShowVo : studentShowVos) {
            String sid = studentShowVo.getId();
            studentShowVo.setCourseName(studentCourseService.listCourseNameBySid(sid));
        }
        return studentShowVos;
    }

    @Override
    public StudentShowVo getStudentByName(String name) {
        StudentShowVo studentShowVoByName = studentMapper.getStudentShowVoByName(name);
        if(studentShowVoByName != null){
            String sid = studentShowVoByName.getId();
            String courseName = studentCourseService.listCourseNameBySid(sid);
            studentShowVoByName.setCourseName(courseName);
        }
        return studentShowVoByName;
    }

    @Override
    public StudentShowVo getStudentVoBySid(String sid) {
        StudentShowVo studentShowVoBySid = studentMapper.getStudentShowVoBySid(sid);
        if(studentShowVoBySid!=null){
            String courseName = studentCourseService.listCourseNameBySid(sid);
            studentShowVoBySid.setCourseName(courseName);
        }
        return studentShowVoBySid;
    }

    @Override
    public List<Student> listStudentByClassId(String classId) {
        return studentMapper.listStudentByClassId(classId);
    }
}
